<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php /* @var $this \Magento\Customer\Block\Adminhtml\Edit\Tab\Addresses */ ?>
<script data-template="tab-address-content" type="text/x-jquery-tmpl">
    <div>
        {{if prefix || firstname || middlename || lastname || suffix}}
        ${prefix} ${firstname} ${middlename} ${lastname} ${suffix}<br/>
        {{/if}}
        {{if company}}${company}<br/>{{/if}}
        {{if street0}}${street0}<br/>{{/if}}
        {{if street1}}${street1}<br/>{{/if}}
        {{if street2}}${street2}<br/>{{/if}}
        {{if street3}}${street3}<br/>{{/if}}
        {{if city || region || postcode}}
        {{if city}}${city}{{/if}}{{if region}}{{if city}}, {{/if}}${region}{{/if}}{{if postcode}}{{if city || region}}, {{/if}}${postcode}{{/if}}<br/>
        {{/if}}
        {{if country_id}}${country_id}<br/>{{/if}}
        {{if telephone}}T: ${telephone}<br/>{{/if}}
        {{if fax}}F: ${fax}<br/>{{/if}}
        {{if vat_id}}VAT: ${vat_id}{{/if}}
    </div>
</script>

<!-- Addresses list -->
<fieldset class="fieldset">
    <legend class="legend">
        <span><?php echo __('Customer Addresses') ?></span>
    </legend>
    <br />
    <?php
    $mageInitializationData = $this->jsonEncode([
        'addressTabs' => [
            'itemCount'                     => count($addressCollection),
            'deleteConfirmPrompt'           => __('Are you sure you want to delete this address?'),
            'regionsUrl'                    => $this->getRegionsUrl(),
            'optionalZipCountries'          => $this->getOptionalZipCountries(),
            'requiredStateForCountries'     => $this->getRequiredStateForCountries(),
            'showAllRegions'                => $this->getShowAllRegions(),
            'defaultCountries'              => $this->getDefaultCountries(),
            'templatePrefix'                => $this->getTemplatePrefix(),
        ]
    ]);
    ?>
    <div id="address-tabs" data-mage-init='<?= $this->escapeHtml($mageInitializationData)?>'>
        <ul class="address-list" id="address_list">
            <?php $_iterator = 0; ?>
            <?php if(count($addressCollection)): ?>
                <?php foreach ($addressCollection as $_address): ?>

                    <li class="address-list-item" id="address_item_<?php echo $_address->getId() ?>" data-item="<?php echo $_address->getId() ?>">
                        <a href="#form_address_item_<?php echo $_address->getId() ?>" data-mage-init='{"dataItemDeleteButton": {}}'>
                            <?php if (!$this->isReadonly()): ?>
                                <div class="address-list-item-actions">
                                    <button class="action-delete" type="button" title="Remove address" id="delete_button<?php echo ++$_iterator ?>" data-role="delete">
                                        <span>Remove address</span>
                                    </button>
                                    <button class="action-edit" type="button" title="Edit address" id="select_button_<?php echo $_address->getId() ?>">
                                        <span>Edit address</span>
                                    </button>
                                </div>
                            <?php endif;?>

                            <address>
                                <?php echo $this->format($_address, 'html') ?>
                            </address>
                        </a>
                        <div class="field field-address-item-billing choice">
                            <input type="checkbox" <?php if ($this->isReadonly()):?> disabled="disabled"<?php endif;?> value="<?php echo $_address->getId() ?>" id="address_item_billing<?php echo $_address->getId() ?>" name="account[default_billing]" title="<?php echo __('Set as Default Billing Address') ?>"<?php if($_address->isDefaultBilling()): ?> checked="checked"<?php endif; ?> />
                            <label class="label" for="address_item_billing<?php echo $_address->getId() ?>">
                                <span><?php echo __('Default Billing Address') ?></span>
                            </label>
                        </div>
                        <div class="field field-address-item-shipping choice">
                            <input type="checkbox" <?php if ($this->isReadonly()):?> disabled="disabled"<?php endif;?> value="<?php echo $_address->getId() ?>" id="address_item_shipping<?php echo $_address->getId() ?>" name="account[default_shipping]" title="<?php echo __('Set as Default Shipping Address') ?>"<?php if($_address->isDefaultShipping()): ?> checked="checked"<?php endif; ?> />
                            <label class="label" for="address_item_shipping<?php echo $_address->getId() ?>">
                                <span><?php echo __('Default Shipping Address') ?></span>
                            </label>
                        </div>
                    </li>
                <?php endforeach; ?>
            <?php endif; ?>
            <li data-container="add-address" class="address-list-actions last"><?php echo $this->getAddNewButtonHtml() ?></li>
        </ul>

        <!-- Template for adding address item to list -->
        <script data-template="address-tab" type="text/x-jquery-tmpl">
            <div id="address_item_template" class="hidden template">
                <li class="address-list-item" id="address_item_${itemId}" data-item="${itemId}">
                    <a href="#form_new_item${itemId}" data-mage-init='{"dataItemDeleteButton": {}}'>
                        <?php if (!$this->isReadonly()): ?>
                            <div class="address-list-item-actions">
                                <button class="action-delete" type="button" title="Remove address" id="delete_button${itemId}" data-role="delete">
                                    <span>Remove address</span>
                                </button>
                                <button class="action-edit" type="button" title="Edit address">
                                    <span>Edit address</span>
                                </button>
                            </div>
                        <?php endif;?>

                        <address>
                            <?php echo __('New Customer Address') ?>
                        </address>
                    </a>
                    <div class="field choice field-address-item-billing">
                        <input type="checkbox" <?php if ($this->isReadonly()):?> disabled="disabled"<?php endif;?> value="_item${itemId}" id="address_item_billing_item${itemId}" name="account[default_billing]" title="<?php echo __('Set as Default Billing Address') ?>" />
                        <label class="label" for="address_item_billing_item${itemId}">
                            <span><?php echo __('Default Billing Address') ?></span>
                        </label>
                    </div>
                    <div class="field choice field-address-item-shipping">
                        <input type="checkbox" <?php if ($this->isReadonly()):?> disabled="disabled"<?php endif;?> value="_item${itemId}" id="address_item_shipping_item${itemId}" name="account[default_shipping]" title="<?php echo __('Set as Default Shipping Address') ?>"/>
                        <label class="label" for="address_item_shipping_item${itemId}">
                            <span><?php echo __('Default Shipping Address') ?></span>
                        </label>
                    </div>
                </li>
            </div>
        </script>

        <!-- Address form template -->
        <?php $_templatePrefix = $this->getTemplatePrefix() ?>
        <script data-template="address-form" type="text/x-jquery-tmpl">
            <div id="address_form_template" class="no-display template"><!-- Don`t delete class no-display, save customer stops work  -->
                <div id="form_${formName}" data-item="${itemCount}" class="address-item-edit-content"
                     data-mage-init='{"observableInputs":{"name": "${formName}"}}'>
                    <?php
                    // Set form template elements prefix
                    $this->getForm()->setHtmlIdPrefix($_templatePrefix)
                        ->setFieldNameSuffix('address['.$_templatePrefix.']');
                    ?>
                    <?php echo $this->getForm()->getHtml() ?>
                </div>
            </div>
        </script>

        <!-- Addresses forms -->
        <div data-container="address-forms" class="address-item-edit" id="address_form_container">
            <?php if(count($addressCollection)): ?>
                <?php
                /** @var \Magento\Customer\Service\V1\Data\Address $_address */
                foreach ($addressCollection as $_address): ?>
                    <div class="address-item-edit-content" id="form_address_item_<?php echo $_address->getId() ?>" data-item="<?php echo $_address->getId() ?>" style="display:none" data-mage-init='{"observableInputs": {"name": "address_item_<?php echo $_address->getId() ?>"}}'>
                        <?php
                        $this->initForm()->getForm()
                            ->addValues(\Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($_address))
                            ->setHtmlIdPrefix("_item{$_address->getId()}")
                            ->setFieldNameSuffix('address['.$_address->getId().']');
                        $this->addValuesToNamePrefixElement($_address->getPrefix())
                            ->addValuesToNameSuffixElement($_address->getSuffix());
                        ?>
                        <?php echo $this->getForm()->getHtml() ?>
                        <input type="hidden" name="address[<?php echo $_address->getId() ?>][_deleted]" id="deleted_address_item_<?php echo $_address->getId() ?>" />
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</fieldset>
